function addToCart(id){
    var obj = tr2obj(id);
    // check if obj already exist
    var rslt = checkCart(obj);
    if(rslt == false){
        return false;
    }
    
    addTotalCost(obj.price);
    
    var cart = document.getElementById("hotel_consume_shopping_cart");
    var len = cart.rows.length;
    
    var row = cart.insertRow(len);
    row.setAttribute("form-data", obj.id);
    var cellList = new Array();
    cellList[0] = row.insertCell(0);
    cellList[0].innerHTML = "<span class='cart_content'>" + obj.title + "</span>"
    cellList[0].setAttribute("form-data", "title");
    cellList[1] = row.insertCell(1);
    cellList[1].innerHTML = "$" + "<span class='cart_content cart_content_price'>" + obj.price + "</span>";
    cellList[1].setAttribute("form-data", "price");
    cellList[2] = row.insertCell(2);
    cellList[2].innerHTML = '<div class="btn-group btn-group-xs">' + 
                    '<button type="button" class="btn btn-white" onclick="cartNumMinus(' + obj.id + ')">-</button>' +
                    '<button type="button" class="btn btn-white"><span class="cart_content_num">1</span></button>' +
                    '<button type="button" class="btn btn-white" onclick="cartNumAdd(' + obj.id + ')">+</button>' +
                    '</div>';
    cellList[2].setAttribute("form-data", "num");
    cellList[3] = row.insertCell(3);
    cellList[3].innerHTML = '<button class="btn btn-danger btn-xs" type="button" onclick="cartItemRemove(' + obj.id + ')">Remove</button>';
    
    return true;
}

function tr2obj(id){
    var trElem = document.getElementById("hotel_consume_id_" + id);
    var tdList = trElem.getElementsByTagName("td");
    
    var tdKeyList = new Array();
    var tdValList = new Array();
    var j = 0;
    for (var i = 0; i < tdList.length; i++) {
        var elem = tdList[i];
        if(elem.getAttribute("form-data")){
            tdKeyList[j] = elem.getAttribute("form-data");
            tdValList[j] = elem.getElementsByClassName("consume_content")[0].innerText;
            j++;
        }
    }
    
    var myJSON = createJSON(tdKeyList, tdValList);
    var myObj = JSON.parse(myJSON);
    return myObj;
}

function checkCart(obj){
    var tbodyElem = document.getElementById("hotel_consume_shopping_cart_tbody");
    var trList = tbodyElem.getElementsByTagName("tr");
    if(trList.length <= 0){
        return true;
    }
    
    for (var i = 0; i < trList.length; i++) {
        var elem = trList[i];
        if(elem.getAttribute("form-data") == obj.id){
            return false;
        }
    }
    return true;
}

function submitCart(){
    var objJSON = cart2JSON();
    ajaxDoc(objJSON);
}

function cart2JSON(){
    var objList = new Array();
    
    var tbodyElem = document.getElementById("hotel_consume_shopping_cart_tbody");
    var trList = tbodyElem.getElementsByTagName("tr");
    for (var i = 0; i < trList.length; i++) {
        var elem = trList[i];
        var numElem = elem.getElementsByClassName("cart_content_num")[0];    
        objList[i] = {
            id : elem.getAttribute("form-data"),
            num : numElem.innerText
        }
    }
    
    return JSON.stringify(objList);
}

function ajaxDoc(objJSON){
    
    xmlhttp = new XMLHttpRequest();
    
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            var text = xmlhttp.responseText;
            alert(text);
            window.location.reload();
        }
    }
    
    xmlhttp.open("POST", "./background/hotel_consume_do.jsp", true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    var send_text = "objJSON=" + objJSON;
    xmlhttp.send(send_text);

}

function createJSON(keyList, valList){
    var key_value_list = new Array();
    for (var i = 0; i < keyList.length; i++) {
        var keyElem = keyList[i];
        var valElem = valList[i];
        key_value_list[i] = "\"" + keyElem + "\" : \"" + valElem + "\"";
    }
    
    var myJSON = "{ " + key_value_list.join(", ") + " }";
    return myJSON;
}

function cartNumMinus(id){
    var tbodyElem = document.getElementById("hotel_consume_shopping_cart_tbody");
    var trList = tbodyElem.getElementsByTagName("tr");
    for (var i = 0; i < trList.length; i++) {
        var elem = trList[i];
        if(elem.getAttribute("form-data") == id){
            var numElem = elem.getElementsByClassName("cart_content_num")[0];
            var tmp = new Number(numElem.innerText);
            numElem.innerText = tmp == 1 ? 1 : tmp - 1;
            
            if(tmp != 1){
                var price = elem.getElementsByClassName("cart_content_price")[0].innerText;
                minusTotalCost(price);
            }
        }
    }
}

function cartNumAdd(id){
    var tbodyElem = document.getElementById("hotel_consume_shopping_cart_tbody");
    var trList = tbodyElem.getElementsByTagName("tr");
    for (var i = 0; i < trList.length; i++) {
        var elem = trList[i];
        if(elem.getAttribute("form-data") == id){
            var numElem = elem.getElementsByClassName("cart_content_num")[0];
            var tmp = new Number(numElem.innerText);
            numElem.innerText = (tmp + 1);
            
            var price = elem.getElementsByClassName("cart_content_price")[0].innerText;
            addTotalCost(price);
        }
    }
}

// remove cart item
function cartItemRemove(id){
    var tbodyElem = document.getElementById("hotel_consume_shopping_cart_tbody");
    var trList = tbodyElem.getElementsByTagName("tr");
    for (var i = 0; i < trList.length; i++) {
        var elem = trList[i];
        if(elem.getAttribute("form-data") == id){
            var numElem = elem.getElementsByClassName("cart_content_num")[0];
            var tmp = new Number(numElem.innerText);
            
            var price = elem.getElementsByClassName("cart_content_price")[0].innerText;
            minusTotalCost(price * tmp);
            
            tbodyElem.removeChild(elem);
        }
    }
}

// modify for total cost
function addTotalCost(price){
    var totalElem = document.getElementById("hotel_consume_shopping_cart_total_cost");
    var oldPrice = new Number(totalElem.innerText);
    
    var newPrice = new Number(oldPrice + new Number(price));
    
    totalElem.innerText = newPrice;
}

function minusTotalCost(price){
    var totalElem = document.getElementById("hotel_consume_shopping_cart_total_cost");
    var oldPrice = new Number(totalElem.innerText);
    
    var newPrice = new Number(oldPrice - new Number(price));
    
    totalElem.innerText = newPrice;
}
